package com.merelyb.business.service.token;

import com.merelyb.data.service.service.mybatis.bean.token.TokenInfo;
import com.merelyb.data.service.service.service.token.TokenInfoService;
import com.merelyb.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by LiM on 2017/12/12.
 */
@Service
public class BusinessTokenInfoService {
    @Autowired
    TokenInfoService tokenInfoService;

    /**
     * 检查token
     *
     * @param tokenInfo
     * @return
     */
    public TokenInfo checkTokenInfo(TokenInfo tokenInfo) {

        TokenInfo tokenInfoResult = null;
        Date date = new Date();
        //首次获取
        if (tokenInfo.getToken() == null) {
            String sUId = UUID.randomUUID().toString().replaceAll("-", "");
            tokenInfo.setToken(sUId);
            tokenInfo.setIsDelete(0);
            tokenInfo.setEffectTime(date.getTime());
            tokenInfo.setCreateTime(date);
            tokenInfo.setTokenType(1);
            tokenInfo.setTokenCycle(2);
            try {
                tokenInfoService.addData(tokenInfo);
                tokenInfoResult = new TokenInfo();
                BASE64Encoder encoder = new BASE64Encoder();
                //需做次加密
                String sToken = JsonUtils.obj2Json(tokenInfo);
                tokenInfoResult.setAccId(tokenInfo.getAccId());
                tokenInfoResult.setEffectTime(tokenInfo.getEffectTime());
                tokenInfoResult.setToken(encoder.encode(sToken.getBytes("UTF-8")));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                return tokenInfoResult;
            }

        } else {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                String sData = new String(decoder.decodeBuffer(tokenInfo.getToken()), "UTF-8");
                //需做次解密
                TokenInfo tokenInfoQuery = JsonUtils.json2Obj(sData, TokenInfo.class);
                List<TokenInfo> tokenInfoList = tokenInfoService.queryAllList(tokenInfoQuery);
                if (tokenInfoList == null || tokenInfoList.size() != 1) {
                    //没有的token
                    tokenInfoResult = tokenInfo;
                    tokenInfoResult.setToken("404");
                } else {
                    tokenInfoQuery = tokenInfoList.get(0);
                    boolean bNew = true;
                    switch (tokenInfoQuery.getTokenType()) {
                        case 0:  //分
                        {
                            bNew = (date.getTime() - tokenInfoQuery.getEffectTime()) < tokenInfoQuery.getTokenCycle() * 60 * 1000;
                            break;
                        }
                        case 1:  //时
                        {
                            bNew = (date.getTime() - tokenInfoQuery.getEffectTime()) < tokenInfoQuery.getTokenCycle() * 60 * 60 * 1000;
                            break;
                        }
                        case 2: //日
                        {
                            bNew = (date.getTime() - tokenInfoQuery.getEffectTime()) < tokenInfoQuery.getTokenCycle() * 24 * 60 * 60 * 1000;
                            break;
                        }
                        default:
                        {
                            bNew = false;
                            break;
                        }
                    }

                    if (!bNew) {
                        //刷新
                        tokenInfoQuery.setIsDelete(1);
                        tokenInfoService.deleteData(tokenInfoQuery);
                        String sUId = UUID.randomUUID().toString().replaceAll("-", "");
                        tokenInfoQuery.setToken(sUId);
                        tokenInfoQuery.setCreateTime(date);
                        tokenInfoQuery.setEffectTime(date.getTime());
                        tokenInfoQuery.setIsDelete(0);
                        tokenInfoService.addData(tokenInfo);

                        tokenInfoResult = new TokenInfo();
                        BASE64Encoder encoder = new BASE64Encoder();
                        //需做次加密
                        String sToken = JsonUtils.obj2Json(tokenInfoQuery);
                        tokenInfoResult.setToken(encoder.encode(sToken.getBytes("UTF-8")));

                    } else {
                        tokenInfoResult = tokenInfo;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                return tokenInfoResult;
            }
        }
    }
}
